// pages/new_branch_lamp/new_branch_lamp.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    auto: false,
    longitude: '',
    latitude: '',
    loadgroupmid: '',
    loadgroupid: '',
    loadname: '',
    districtid: '',
    districtmid: '',
    districtname: '',
    branchmid:'',
    branchid:'',
    branchname:'',

    branchlampname: '',
    lampbrand: '',
    branchlampno: '',
   
    controllerno: '',   
    index: 0,
    groupID: -1,
    lamp_type_array: [
      {
        id: "0",
        name: "普通灯"
      },
      {
        id: "1",
        name: "十字路口灯"
      },
      {
        id: "2",
        name: "主干道灯"
      },
      {
        id: "3",
        name: "人行道灯"
      },
      {
        id: "4",
        name: "隧道灯"
      }
    ]   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      loadgroupmid: options.loadgroupmid,
      loadgroupid: options.loadgroupid,
      loadname: options.loadname,
      districtid: options.districtid,
      districtmid: options.districtmid,
      districtname: options.districtname,
      branchmid: options.branchmid,
      branchid: options.branchid,
      branchname: options.branchname
    });
  },


  formSubmit: function (e) {
    let that = this;
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
   
    if (e.detail.value.input_lampname == "") {
      wx.showModal({
        title: '提示',
        content: '单灯名称不能为空',
        showCancel: false
      });

      return;
    }

    that.setData({     
      branchlampname: e.detail.value.input_branchlampname,
      lampbrand: e.detail.value.input_brand,
      branchlampno: e.detail.value.input_branchlampno,
      index: e.detail.value.picker_type
    });   

    wx.request({
      url: 'https://119.3.46.145/server/AddLightOnBranch?Name=' + that.data.branchlampname + '&LightBrand=' + that.data.lampbrand + '&LightNo=' + that.data.branchlampno + '&LightType=' + that.data.index + '&BranchID=' + that.data.branchmid,
  
      method: 'Post',
      // data: {
      //   Name: that.data.branchlampname,
      //   LightBrand: that.data.lampbrand,
      //   LightNo: that.data.branchlampno,
      //   LightType: that.data.index,
      //   BranchID: that.data.branchmid
      // },
      header: header,
      success: res => {
        //data: { error: "0"}  {error: "-1"}        
        let result = res.data;
        //console.log(result, "reuslt");

      

        // let error = JSON.parse(response).error;
        // if (error == 0) {
        //   refreshLightsOfBranchTable(selected_node.attributes.mid);
        //   alert("添加成功");
        // }
        // else {
        //   alert("添加失败");
        // }

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: "添加成功",
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/FindLightOnBranch?id=' + that.data.branchmid,
                header: header,
                success: function (res) {
                  let data = JSON.parse(res.data.data);

                  if (data.length == 0) {
                    return;
                  }

                  data.forEach(function (item, index, self) {
                    item.isSelect = false;
                  });

                  //console.log(data, "data loadgroup")
                  let tmp_branch_lamp_list = [];

                  data.forEach(function (item, index, self) {
                    item.isSelect = false;
                    tmp_branch_lamp_list.push(item);
                  });

                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    branch_lamp_list: tmp_branch_lamp_list
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });
        }

        if (result.error == "-1") {
          wx.showModal({
            title: '提示',
            content: "添加失败",
            showCancel: false
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    that.setData({
      loadgroupName: '',
      describe: '',
      name: '',
      telephone: '',
      longitude: '',
      latitude: '',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})